package org.learn.intermediate.houserent.view;

import org.learn.intermediate.houserent.domain.House;
import org.learn.intermediate.houserent.service.HomeService;

import java.util.Scanner;

//显示界面，接受用户输入，调用houseservice方法完成房屋信息的各种操作
public class HomeView {

    public boolean loop = true;//控制显示菜单
    private char key = ' ';//接受用户输入
    private HomeService houseService = new HomeService(10);


//    //根据id修改房屋信息
//    public void update(){
//        System.out.println("请输入待修改的房屋的编号（-1退出）");
//        int update = new Scanner(System.in).nextInt();
//        if(update == -1) {
//            System.out.println("==========放弃修改房屋信息==========");
//            return;
//        }
//        House house = houseService.findByID
//        if(house == )
//    }


    //编写delid方法
    public void delHouse() {
        System.out.println("==========删除房屋信息==========");
        System.out.println("请输入待删除的房屋的编号（-1退出）");
        int delid = new Scanner(System.in).nextInt();
        if(delid == -1) {
            System.out.println("==========放弃删除房屋信息==========");
            return;
        }
        if(houseService.del(delid)) {
            System.out.println("==========删除房屋信息成功==========");
        }else {
            System.out.println("==========房屋编号不存在==========");
        }
    }

    //编写addhouse方法接受输入，创建house对象，调用add方法
    public void addHouse(){
        System.out.println("==========添加房屋==========");
        System.out.println("姓名 ：");
        String name = new Scanner(System.in).next();
        System.out.println("电话 ：");
        String phone = new Scanner(System.in).next();
        System.out.println("地址 ：");
        String address = new Scanner(System.in).next();
        System.out.println("月租 ：");
        int rent = new Scanner(System.in).nextInt();
        System.out.println("状态 ：");
        String state = new Scanner(System.in).next();

        //创建一个新的house对象
        House newHouse = new House(0,name,phone,address,rent,state);
        if(houseService.add(newHouse)) {
            System.out.println("==========添加房屋成功==========");
        }else{
            System.out.println("==========添加房屋失败==========");
        }
    }

    //编写listhouse（）显示房屋列表
    public void listhouses() {
        System.out.println("==========房屋列表==========");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(已出租/未出租)");
        House[] houses = houseService.list();//得到所有房屋信息
        for (int i = 0; i < houses.length; i++) {
            if(houses[i] == null){
                break;
            }
            System.out.println(houses[i]);
        }
        System.out.println(("==========房屋列表显示完毕=========="));
    }
    //显示主菜单
    public void mainMenu(){

        do{
            System.out.println("==========房屋出租系统==========");
            System.out.println("\t\t\t1 新 增 房 源");
            System.out.println("\t\t\t2 查 找 房 源");
            System.out.println("\t\t\t3 删 除 房 屋 信 息");
            System.out.println("\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t6 退      出");
            System.out.println("请输入你的选择（1-6）:");
             String key =  new Scanner(System.in).next();
            switch (key){
                case "1" :
                   addHouse();
                    break;
                case "2" :
                    System.out.println("查找");
                    break;
                case "3" :
                   delHouse();
                    break;
                case "4" :
                    System.out.println("修改");
                    break;
                case "5" :
                   listhouses();
                    break;
            }
        }while (loop);
    }
}
